package com.hope.hotel.controller;

import com.hope.hotel.common.Result;
import com.hope.hotel.entity.po.Message;
import com.hope.hotel.entity.vo.MessageVO;
import com.hope.hotel.service.MessageService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 消息通知控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/message")
public class MessageController {
    
    private final MessageService messageService;
    
    /**
     * 获取用户消息列表
     */
    @GetMapping("/list")
    public Result<List<MessageVO>> getUserMessages(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        return messageService.getUserMessages(userId, page, pageSize);
    }
    
    /**
     * 标记消息为已读
     */
    @PostMapping("/read/{messageId}")
    public Result markAsRead(@PathVariable Integer messageId, HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        return messageService.markAsRead(messageId, userId);
    }
    
    /**
     * 删除消息
     */
    @DeleteMapping("/{messageId}")
    public Result deleteMessage(@PathVariable Integer messageId, HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        return messageService.deleteMessage(messageId, userId);
    }
    
    /**
     * 获取未读消息数量
     */
    @GetMapping("/unread-count")
    public Result<Integer> getUnreadCount(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        return messageService.getUnreadCount(userId);
    }
    
    /**
     * 创建系统消息（仅管理员可用）
     */
    @PostMapping("/create")
    public Result createMessage(@RequestBody Message message) {
        message.setCreateTime(LocalDateTime.now());
        message.setIsRead(false);
        return messageService.createMessage(message);
    }
} 